<?php

namespace Common\Service;

use Common\Service\ApiService;

class UploadService {

  private static $instance;
  var $error;
  var $root_path = './Public/upload/tmp/';

  public static function instance() {
    if (self::$instance == null) {
      $instance = new UploadService();
    }

    return $instance;
  }

  function set_error($err) {
    $this->error = $err;
  }

  function get_error() {
    return $this->error;
  }

  function upload() {
    $config = array(
      'maxSize' => 2097152,
      'exts' => array('jpg', 'gif', 'png', 'jpeg'),
      //'savePath' => '../Public/tmp/',
      'savePath' => '',
      'rootPath' => './Public/upload/tmp/',
      'autoSub' => true,
      'subName' => array('date', 'Ymd'),
    );

    $upload = new \Think\Upload($config);

    $info = $upload->upload();


    if (!$info) { // 上传错误提示错误信息
      $this->set_error($upload->getError());
      return FALSE;
    } else {// 上传成功
      $fileinfo = $info['file'];
      $fileinfo['fullpath'] = $this->root_path . $fileinfo['savepath'] . $fileinfo['savename'];
      return $fileinfo;
    }
  }

  function do_upload($type = 'icon' ) {
    switch ($type) {
      case 'icon' :
        return $this->_do_upload('icon');
      case 'kindeditor':
        $ret = $this->_do_upload('icon');
        //print_r($ret);
        if ($ret['status']) {
          //上传成功
          return array(
            'error' => 0,
            'url' => $ret['data']['ab_path'],
          );
        } else {
          return array(
            'error' => 1,
            'message' => $ret['info'],
          );
        }
      case 'attach':
        return $this->_do_upload('attach');
      case 'tmp' :
        return $this->_do_upload('tmp');
    }
  }

  function _do_upload($save_path = 'icon') {
    $config = array(
      'maxSize' => 2097152,
      'rootPath' => "./Public/upload/$save_path/",
      'savePath' => '',
      'saveName' => array('uniqid', ''),
      'exts' => array('jpg', 'gif', 'png', 'jpeg','xls','xlsx','doc','docx'),
      'autoSub' => true,
      'subName' => array('date', 'Ym'),
    );

    $upload = new \Think\Upload($config); // 实例化上传类 

    $path = "./Public/upload/$save_path/" . date('Ym');

    if (!file_exists($path)) {
      mkdir($path, 0777);
    }

    $info = $upload->upload();
    if (!$info) {
      // 上传错误提示错误信息
      return ajax_arr($upload->getError() . $path, FALSE);
    } else {
      $img_path = "Public/upload/$save_path/" . $info['imgFile']['savepath'] . $info['imgFile']['savename'] ;
      $ret['data'] = array(
        'ab_path' => U( $img_path , '' , TRUE , TRUE ) ,
        'savePath' => "Public/upload/$save_path/" . $info['imgFile']['savepath'] . $info['imgFile']['savename'],
        'type' => $info['imgFile']['type'],
        'size' => $info['imgFile']['size'],
        'name' => $info['imgFile']['savename'],
      );
            
      return ajax_arr("上传成功", TRUE, $ret);
    }
  }

  function check_img($imgpath) {

    $image = new \Think\Image();
    $image->open($imgpath);

    $width = $image->width(); // 返回图片的宽度
    $height = $image->height(); // 返回图片的高度


    if ($width == 600 && $height == 500) {
      return ajax_arr('OK', TRUE);
    } else {
      //错误
      delete_file($imgpath);
      return ajax_arr('图片必须600*500px', FALSE);
    }
  }

}
